/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-06 06:29:04
 * @LastEditTime: 2022-07-17 22:11:28
 * @LastEditors: liutq
 * @Reference:
 */
/*
 * @Description:
 * @Author: maxyang
 * @Date: 2022-06-06 06:29:04
 * @LastEditTime: 2022-06-06 09:37:00
 * @LastEditors: liutq
 * @Reference:
 */
package admin

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/maxyang107/jinglan/common"
	"github.com/maxyang107/jinglan/service"
	"github.com/maxyang107/jinglan/service/admin"
	"github.com/maxyang107/jinglan/utils"
)

/**
 * @description: 方法描述：保存商品
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func AddGoods(c *gin.Context) {
	var addGoodsParam common.Goods

	if err := c.ShouldBind(&addGoodsParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")

	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	addGoodsParam.GoodsAuthorUid = utils.InterfaceToUser(userid)

	err := admin.AdminGoodsService.AddGoods(&addGoodsParam)

	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseToJson(http.StatusOK, common.Ok, "商品保存成功", c)
}

/**
 * @description: 方法描述：修改商品
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func ModifyGoods(c *gin.Context) {
	var addGoodsParam common.Goods

	if err := c.ShouldBind(&addGoodsParam); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	userid, exists := c.Get("UserId")

	if !exists {
		common.ResponseToJson(http.StatusOK, common.NotAuth, "token解析失败", c)
		return
	}

	addGoodsParam.GoodsAuthorUid = utils.InterfaceToUser(userid)
	err := admin.AdminGoodsService.ModifyGoods(&addGoodsParam)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseToJson(http.StatusOK, common.Ok, "商品修改成功", c)
}

/**
 * @description: 方法描述：获取藏品列表
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetGoodsList(c *gin.Context) {
	var goodslist common.AdminGoodsList

	if err := c.BindQuery(&goodslist); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	gl := admin.AdminGoodsService.GetGoodsList(goodslist)

	common.ResponseListToJson(http.StatusOK, int(gl.Page), int(gl.PerPage), int(gl.Total), gl.Data, c)
}

/**
 * @description: 方法描述：获取商品详情
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetGoodsDetail(c *gin.Context) {
	var goodsdetail common.Detail
	if err := c.BindQuery(&goodsdetail); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}
	gt := admin.AdminGoodsService.GetGoodsDetail(goodsdetail.Id)

	common.ResponseDetailToJson(http.StatusOK, gt, "success!", c)
}

/**
 * @description: 方法描述：开签
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func OpenDraw(c *gin.Context) {
	var openDraw common.OpenDraw
	if err := c.ShouldBind(&openDraw); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	err := admin.AdminGoodsService.OpenDraw(openDraw.GoodsId, openDraw.Opennum)
	if err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	common.ResponseToJson(http.StatusOK, common.Ok, "success！", c)
}

func GetDrawTotal(c *gin.Context) {
	var param common.Detail
	if err := c.BindQuery(&param); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	u := admin.AdminGoodsService.GetDrawTotal(param.Id)
	common.ResponseDetailToJson(http.StatusOK, u, "success", c)

}

/**
 * @description: 方法描述：拉取微店商品信息
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func GetVshopGoodsList(c *gin.Context) {
	var goodslist common.AdminGoodsList

	if err := c.BindQuery(&goodslist); err != nil {
		common.ResponseToJson(http.StatusOK, common.ParamError, err.Error(), c)
		return
	}

	v, err := admin.AdminGoodsService.GetVshopGoodsList(goodslist)

	if err != nil {
		common.ResponseToJson(http.StatusOK, common.Fail, err.Error(), c)
		return
	}

	common.ResponseListToJson(http.StatusOK, int(goodslist.Page), int(goodslist.PerPage), v.Result.TotalNum, v, c)
}

/**
 * @description: 方法描述：重试退款
 * @Author: maxyang
 * @return {*}
 * @param {*gin.Context} c
 */
func RetryRefound(c *gin.Context) {
	service.GoodsService.RetryRefount()
	common.ResponseToJson(http.StatusOK, common.Ok, "重试退款成功！", c)
}
